package cc.rengu.redp.bizimpl.csmc.service.impl;

import cc.rengu.redp.bizimpl.csmc.dao.MchntFeeInfoReportMapper;
import cc.rengu.redp.bizimpl.csmc.domain.TCMchntAcctSum;
import cc.rengu.redp.bizimpl.csmc.service.MchntFeeInfoReportService;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
@Slf4j
public class MchntFeeInfoReportServiceImpl extends ServiceImpl<MchntFeeInfoReportMapper, TCMchntAcctSum> implements MchntFeeInfoReportService {

    @Autowired
    private MchntFeeInfoReportService mchntFeeInfoReportService;

    /**
     *商户手续费统计报表
     */
    @Override
    public IPage<TCMchntAcctSum> findPage(QueryRequest request, TCMchntAcctSum tcMchntAcctSum) {

        LambdaQueryWrapper<TCMchntAcctSum> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(tcMchntAcctSum.getInstId())) {
            queryWrapper.eq(TCMchntAcctSum::getInstId, tcMchntAcctSum.getInstId());
        }
        //
        if (!StringUtils.isBlank(tcMchntAcctSum.getStartDate())) {
            queryWrapper.ge(TCMchntAcctSum::getBatchDate, tcMchntAcctSum.getEndDate());
        }
        if (!StringUtils.isBlank(tcMchntAcctSum.getEndDate())) {
            queryWrapper.le(TCMchntAcctSum::getBatchDate, tcMchntAcctSum.getEndDate());
        }
        if (!StringUtils.isBlank(tcMchntAcctSum.getAcqInstCode())) {
            queryWrapper.eq(TCMchntAcctSum::getAcqInstCode, tcMchntAcctSum.getAcqInstCode());
        }
        if (!StringUtils.isBlank(tcMchntAcctSum.getMchntNo())) {
            queryWrapper.eq(TCMchntAcctSum::getMchntNo, tcMchntAcctSum.getMchntNo());
        }
        queryWrapper.orderByDesc(TCMchntAcctSum::getBatchDate);

        //分页
        Page<TCMchntAcctSum> page = new Page<>();
        SortUtil.handlePageSort(request, page, true);
        Page<TCMchntAcctSum> cfgPage = this.page(page, queryWrapper);
        return cfgPage;
    }

    //导出
    @Override
    public IPage<TCMchntAcctSum> findTCMchntAcctSumDetail(QueryRequest request, TCMchntAcctSum tcMchntAcctSum) {
        try {
            Page<TCMchntAcctSum> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            IPage<TCMchntAcctSum> pageTCMchntAcctSum =this.findPage(request, tcMchntAcctSum);
            return pageTCMchntAcctSum;
        } catch (Exception e) {
            log.error("获取信息失败", e);
            return null;
        }
    }
}
